﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace SD.DataAccess
{
    public static class TeamDao
    {
        // object data context use to connect with data base
        private static readonly ServiceDBDataContext dataContext = new ServiceDBDataContext();

        /// <summary>
        /// add new team to database
        /// </summary>
        /// <param name="newTeam"> an instance of team class </param>
        /// <returns> true or false </returns>
        public static bool AddTeam(Team newTeam)
        {
            try
            {
                //insert record to database and save change
                dataContext.Teams.InsertOnSubmit(newTeam);
                dataContext.SubmitChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// edit new team to database
        /// </summary>
        /// <param name="newTeam"> an instance of team class </param>
        /// <returns> true or  false </returns>
        public static bool AmendTeam(Team newTeam)
        {
            //get team if satisfied from database to check exist team id, then change information of team and save change
            Team team = dataContext.Teams.FirstOrDefault(item => item.TeamId == newTeam.TeamId);
            if (team != null)
            {
                BusinessType businessType = newTeam.BusinessId != null
                                           ? dataContext.BusinessTypes.FirstOrDefault(b => b.BusinessId == newTeam.BusinessId)
                                           : null;
                Contact contact = newTeam.ContactId != null
                                      ? dataContext.Contacts.FirstOrDefault(c => c.ContactId == newTeam.ContactId)
                                      : null;
                Address address = newTeam.AddressId != null
                                      ? dataContext.Addresses.FirstOrDefault(a => a.AddressId == newTeam.AddressId)
                                      : null;
                Country country = newTeam.CountryId != null
                                      ? dataContext.Countries.FirstOrDefault(c => c.CountryId == newTeam.CountryId)
                                      : null;
                Department department = newTeam.DepartmentId != null
                                      ? dataContext.Departments.FirstOrDefault(c => c.DepartmentId == newTeam.DepartmentId)
                                      : null;

                team.TeamName = newTeam.TeamName;
                team.BusinessId = newTeam.BusinessId;
                team.BusinessType = businessType;
                team.Address = address;
                team.AddressId = newTeam.AddressId;
                team.CityTown = newTeam.CityTown;
                team.Contact = contact;
                team.ContactId = newTeam.ContactId;
                team.Country = country;
                team.CountryId = newTeam.CountryId;
                team.County = newTeam.County;
                team.Department = department;
                team.DepartmentId = newTeam.DepartmentId;
                team.ShortDescription = newTeam.ShortDescription;
                team.FullDescription = newTeam.FullDescription;
                team.Email = newTeam.Email;
                team.Fax = newTeam.Fax;
                team.PhoneNumber = newTeam.PhoneNumber;
                team.WebAddress = newTeam.WebAddress;
                team.IsActive = newTeam.IsActive;
                try
                {
                    dataContext.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// edit status team to database
        /// </summary>
        /// <param name="teamId"> teamIdwant to edit status </param>
        /// <param name="status"> status program want to change </param>
        /// <returns> true or false </returns>
        public static bool ChangeTeamStatus(int teamId, bool status)
        {
            //get team if satisfied from database to check exist team id, then change status of team and save change
            Team newTeam = dataContext.Teams.FirstOrDefault(item => item.TeamId == teamId);
            if (newTeam != null)
            {
                try
                {
                    newTeam.IsActive = status;
                    dataContext.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// get list Team 
        /// </summary>
        /// <param name="status"> status want to select </param>
        /// <param name="filter"> type of filter </param>
        /// <returns> list Team </returns>
        public static IQueryable<Team> ListTeam(bool status, Collection<string> filter)
        {
            IQueryable<Team> newTeam;
            if (filter == null)
            {
                //show Team have IsActive = true
                if (status == true)
                {
                    newTeam = dataContext.Teams;
                }
                // show all Team
                else
                {
                    newTeam = dataContext.Teams.Where(item => item.IsActive == true).OrderBy(item => item.TeamId);
                }
            }
            else
            {
                //show Team have IsActive = true
                if (status == true)
                {
                    newTeam = dataContext.Teams.Where(item => filter.Contains(item.TeamName[0].ToString())).OrderBy(item => item.TeamId);
                }
                // show all Team
                else
                {
                    newTeam = dataContext.Teams.Where(item => item.IsActive == true && filter.Contains(item.TeamName[0].ToString())).OrderBy(item => item.TeamId);
                }
            }
            return newTeam;
        }

        public static IQueryable<Team> ListTeamByDepartment(bool status, Collection<string> filter, int departmentId)
        {
            IQueryable<Team> newTeam;
            if (filter == null)
            {
                //show Team have IsActive = true by department
                if (status == true)
                {
                    newTeam = dataContext.Teams.Where(item => item.DepartmentId == departmentId);
                }
                // show all Team by department
                else
                {
                    newTeam = dataContext.Teams.Where(item =>
                        item.IsActive == true && item.DepartmentId == departmentId).OrderBy(item => item.TeamId);
                }
            }
            else
            {
                //show Team have IsActive = true by department
                if (status == true)
                {
                    newTeam = dataContext.Teams.Where(item => item.DepartmentId == departmentId &&
                        filter.Contains(item.TeamName[0].ToString())).OrderBy(item => item.TeamId);
                }
                // show all Team by department
                else
                {
                    newTeam = dataContext.Teams.Where(item => item.DepartmentId == departmentId &&
                        item.IsActive == true && filter.Contains(item.TeamName[0].ToString())).OrderBy(item => item.TeamId);
                }
            }
            return newTeam;
        }

        /// <summary>
        /// get Team selected
        /// </summary>
        /// <param name="TeamId"> TeamId selected </param>
        /// <returns> Team </returns>
        public static Team GetTeam(int teamId)
        {
            Team newTeam = dataContext.Teams.FirstOrDefault(item => item.TeamId == teamId);
            return newTeam;
        }

        /// <summary>
        /// get tean by name
        /// </summary>
        /// <param name="teamName">name of team</param>
        /// <returns>team object</returns>
        public static Team GetTeamByName(string teamName)
        {
            Team newTeam = dataContext.Teams.FirstOrDefault(item => item.TeamName== teamName);
            return newTeam;
        }

        /// <summary>
        /// check name has already existed ?
        /// </summary>
        /// <param name="name"> name want to check </param>
        /// <returns> true or false </returns>
        public static bool CheckNameTeam(string name)
        {
            //get team if satisfied from database to check exist team name and return true if exist, false if not exist
            Team newTeam = dataContext.Teams.FirstOrDefault(item => item.TeamName == name);
            if (newTeam != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// check name has already existed?
        /// get team by id. if not exist return false. else
        /// if name of team = name input, return true.
        /// else check that name input exist in DB?
        /// if exist return false, else return true
        /// </summary>
        /// <param name="name">name of team</param>
        /// <param name="id">id of team</param>
        /// <returns></returns>
        public static bool CheckNameTeam(string name, int id)
        {
            //get team if satisfied from database to check exist team id.
            Team newTeam = dataContext.Teams.FirstOrDefault(item => item.TeamId == id);
            //if exist, check name of team = name input?
            if (newTeam != null)
            {
                if (newTeam.TeamName == name)
                    return true;
                else
                {
                    if (CheckNameTeam(name) == true)
                        return false;
                    else
                        return true;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
